package com.rds.workflow.service.impl;

import com.rds.common.utils.DateUtils;
import com.rds.common.utils.SecurityUtils;
import com.rds.common.utils.uuid.SnowFlakeUtils;

import com.rds.workflow.controller.RdaWorkflowController;
import com.rds.workflow.domain.*;
import com.rds.workflow.mapper.*;
import com.rds.workflow.service.IRdaWorkflowService;
import com.rds.workflow.utils.WorkflowStateid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class RdaWorkflowServiceImpl implements IRdaWorkflowService {
    @Autowired
    private RdaWorkflowMapper rdaWorkflowMapper;

    @Autowired
    private RdaWorkflow2NodeRelMapper rdaWorkflow2NodeRelMapper;

    @Autowired
    private RdaNodeTempleteMapper rdaNodeTempleteMapper;

    @Autowired
    private RdaWorkflow2NodeTempleteRelMapper rdaWorkflow2NodeTempleteRelMapper;

    @Autowired
    private RdaWfNodeMapper rdaWfNodeMapper;


    @Autowired
    private RdaWorkflowTempleteMapper rdaWorkflowTempleteMapper;

    /**
     * 根据id查询流程对象
     * @param id 流程对象ID
     * @return 流程对象
     */
    @Override
    public RdaWorkflow selectRdaWorkflowById(Long id) {
        return rdaWorkflowMapper.selectRdaWorkflowById(id);
    }

    /**
     * 查询流程对象列表
     * @param rdaWorkflow 查询流程列表
     * @return 流程列表集合
     */
    @Override
    public List<RdaWorkflow> selectRdaWorkflowist(RdaWorkflow rdaWorkflow) {
        return rdaWorkflowMapper.selectRdaWorkflowist(rdaWorkflow);
    }

    /**
     * 新增流程列表
     * @param rdaWorkflow 流程列表
     * @return 结果
     */
    @Override
    public int insertRdaWorkflow(RdaWorkflow rdaWorkflow) {
        int row=rdaWorkflowMapper.insertRdaWorkflow(rdaWorkflow);
        //后续改成查询流程模板根据assignmentypeid查询
        List<RdaNodeTemplete> list=rdaNodeTempleteMapper.selectRdaNodeTempleteByWorkflowid(rdaWorkflow.getFlowTempleteid());//查询流程模板节点表
        HashMap<Long,Long> map=new HashMap<Long,Long>();
        ArrayList<HashMap> idList=new ArrayList<HashMap>();
        if(list!=null) {
            for (RdaNodeTemplete rdaNodeTemplete : list) {
                //插入流程列表节点
                Long oldId = rdaNodeTemplete.getId();
                Long id = SnowFlakeUtils.nextId();
                map.put(oldId, id);
                idList.add(map);
                RdaWfNode rdaWfNode = new RdaWfNode();
                rdaWfNode.setId(id);
                rdaWfNode.setNodeName(rdaNodeTemplete.getNodeName());
                rdaWfNode.setDescription(rdaNodeTemplete.getDescription());
                rdaWfNode.setLevel(rdaNodeTemplete.getLevel());
                rdaWfNode.setWorkflowid(rdaWorkflow.getId());
                rdaWfNode.setAncestors(rdaNodeTemplete.getAncestors());
                rdaWfNode.setRdorder(rdaNodeTemplete.getRdorder());
                rdaWfNode.setUserId(rdaNodeTemplete.getUserId());
                rdaWfNode.setRoleId(rdaNodeTemplete.getRoleId());
                rdaWfNode.setUserName(rdaNodeTemplete.getUserName());
                rdaWfNode.setRoleName(rdaNodeTemplete.getRoleName());
                rdaWfNode.setOwnerid(SecurityUtils.getUserId());
                rdaWfNode.setTypeid(WorkflowStateid.wfNodeTypeid.intValue());
                rdaWfNode.setPolicyid(WorkflowStateid.wfNodePolicyid.intValue());
                rdaWfNode.setStateid(WorkflowStateid.wfNodeCreateid.intValue());
                rdaWfNode.setNodeType(rdaNodeTemplete.getNodeType());
                rdaWfNode.setUserandor(rdaNodeTemplete.getUserandor());
                rdaWfNode.setNodeandor(rdaNodeTemplete.getNodeandor());
                rdaWfNode.setRejectTostart(rdaNodeTemplete.isRejectTostart());
                rdaWfNode.setRejectToprevious(rdaNodeTemplete.isRejectToprevious());
                rdaWfNode.setRejectSkipComplete(rdaNodeTemplete.isRejectSkipComplete());
                rdaWfNode.setAppovedScript(rdaNodeTemplete.getAppovedScript());
                rdaWfNode.setRejectAutomatic(rdaNodeTemplete.isRejectAutomatic());
                rdaWfNode.setRejecttostartScript(rdaNodeTemplete.getRejecttostartScript());
                //2022-01-15 新增创建时间记录到流程节点表 start
                rdaWfNode.setCreateTime(DateUtils.getNowDate());
                //2022-01-15 新增创建时间记录到流程节点表 end
                //2022-01-24 新增是否允许撤回、是否允许委托 start
                rdaWfNode.setIsWithdraw(rdaNodeTemplete.getIsWithdraw());
                rdaWfNode.setIsAgent(rdaNodeTemplete.getIsAgent());
                //2022-01-24 新增是否允许撤回、是否允许委托 end
                //2022-01-28新增记录流程模板id、流程节点id start
                rdaWfNode.setWorkflowtempleteid(rdaNodeTemplete.getWorkflowid());
                rdaWfNode.setWorkflowtempleteNodeid(rdaNodeTemplete.getId());
                //2022-01-28新增记录流程模板id、流程节点id end
                //2022-02-15 新增xy轴坐标 start
                rdaWfNode.setX(rdaNodeTemplete.getX());
                rdaWfNode.setY(rdaNodeTemplete.getY());
                //2022-02-15 新增xyx轴坐标 end
                //2022-05-06 新增条件1~5 start
                rdaWfNode.setScript1(rdaNodeTemplete.getScript1());
                rdaWfNode.setScript2(rdaNodeTemplete.getScript2());
                rdaWfNode.setScript3(rdaNodeTemplete.getScript3());
                rdaWfNode.setScript4(rdaNodeTemplete.getScript4());
                rdaWfNode.setScript5(rdaNodeTemplete.getScript5());
                //2022-05-06 新增条件1~5 end
                //2022-11-07 新增邮箱
                rdaWfNode.setEmail(rdaNodeTemplete.getEmail());
                rdaWfNodeMapper.insertRdaWfNode(rdaWfNode);
            }


            List<RdaWorkflow2NodeTempleteRel> Rel_list=rdaWorkflow2NodeTempleteRelMapper.selectRdaNoderelTempleteByWorkflowid(rdaWorkflow.getFlowTempleteid());
            if(Rel_list!=null && Rel_list.size()!=0 ){

                for (RdaWorkflow2NodeTempleteRel rdaNodeRELTemplete:Rel_list) {
                    //插入流程列表节点
                    Long id = SnowFlakeUtils.nextId();
                    Long fromid=rdaNodeRELTemplete.getFromid();
                    Long toid=rdaNodeRELTemplete.getToid();
                    for (int i=0;i<idList.size();i++){
                        HashMap<Long,Long> relmap=idList.get(i);
                        for (Long key:relmap.keySet()) {
                            if(key.equals(fromid)){
                                fromid=map.get(key);
                            }
                            if(toid.equals(key)){
                                toid=map.get(key);
                            }
                        }

                    }
                    RdaWorkflow2NodeRel rdaWorkflow2NodeRel=new RdaWorkflow2NodeRel();
                    rdaWorkflow2NodeRel.setId(id);
                    rdaWorkflow2NodeRel.setFromid(fromid);
                    rdaWorkflow2NodeRel.setToid(toid);
                    rdaWorkflow2NodeRel.setWorkflowid(rdaWorkflow.getId());
                    rdaWorkflow2NodeRel.setOperationType(rdaNodeRELTemplete.getOperationType());
                    rdaWorkflow2NodeRel.setOperationState(rdaNodeRELTemplete.getOperationState());
                    rdaWorkflow2NodeRel.setRejectScript(rdaNodeRELTemplete.getRejectScript());
                    rdaWorkflow2NodeRel.setRoad(rdaNodeRELTemplete.getRoad());
                    rdaWorkflow2NodeRel.setRoadFlag(rdaNodeRELTemplete.getRoadFlag());
                    rdaWorkflow2NodeRel.setRoadAuto(rdaNodeRELTemplete.getRoadAuto());
                    rdaWorkflow2NodeRel.setRoadAttribute(rdaNodeRELTemplete.getRoadAttribute());
                    rdaWorkflow2NodeRel.setAppovedScript(rdaNodeRELTemplete.getAppovedScript());
                    rdaWorkflow2NodeRelMapper.insertRdaWorkflow2NodeRel(rdaWorkflow2NodeRel);
                }

            }
        }
        return row;
    }

    /**
     * 修改流程列表
     * @param rdaWorkflow 流程列表
     * @return 结果
     */
    @Override
    public int updateRdaWorkflow(RdaWorkflow rdaWorkflow) {
        return rdaWorkflowMapper.updateRdaWorkflow(rdaWorkflow);
    }

    /**
     * 批量删除流程列表
     * @param ids 需要删除的流程列表ID
     * @return 结果
     */
    @Override
    public int deleteRdaWorkflowByIds(Long[] ids) {
        return rdaWorkflowMapper.deleteRdaWorkflowByIds(ids);
    }

    @Override
    public int deleteRdaWorkflowById(Long id) {
        return rdaWorkflowMapper.deleteRdaWorkflowById(id);
    }

    @Override
    public List<RdaWorkflow> selectRdaWorkflowByStateids(Long userid, String stateids,String searchValue) {
        return rdaWorkflowMapper.selectRdaWorkflowByStateids(userid,stateids,searchValue);
    }
}
